# 启动应用脚本
# 使用方法: .\scripts\start.ps1

Write-Host "🚀 启动视频会议应用..." -ForegroundColor Cyan

# 检查PM2
try {
    pm2 --version | Out-Null
} catch {
    Write-Host "❌ 未安装PM2，请先运行: npm install -g pm2" -ForegroundColor Red
    exit 1
}

# 检查是否已经在运行
$running = pm2 list | Select-String "video-conference"
if ($running) {
    Write-Host "⚠️  应用已在运行" -ForegroundColor Yellow
    Write-Host "是否要重启应用? (Y/N)" -ForegroundColor Yellow
    $response = Read-Host
    if ($response -eq 'Y' -or $response -eq 'y') {
        Write-Host "♻️  重启应用..." -ForegroundColor Yellow
        pm2 restart ecosystem.config.js
    } else {
        Write-Host "取消操作" -ForegroundColor Gray
        exit 0
    }
} else {
    # 启动应用
    Write-Host "▶️  启动应用..." -ForegroundColor Green
    pm2 start ecosystem.config.js
    
    if ($LASTEXITCODE -eq 0) {
        # 保存PM2配置
        pm2 save
        
        Write-Host "`n✅ 应用启动成功！" -ForegroundColor Green
        Write-Host "`n应用访问地址:" -ForegroundColor Cyan
        Write-Host "  Web前端: http://localhost:3100" -ForegroundColor White
        Write-Host "  API服务: http://localhost:3101" -ForegroundColor White
        Write-Host "`n常用命令:" -ForegroundColor Cyan
        Write-Host "  查看状态: pm2 status" -ForegroundColor White
        Write-Host "  查看日志: pm2 logs" -ForegroundColor White
        Write-Host "  停止应用: pm2 stop all" -ForegroundColor White
        Write-Host "  重启应用: pm2 restart all" -ForegroundColor White
    } else {
        Write-Host "❌ 应用启动失败，请查看日志" -ForegroundColor Red
        pm2 logs --lines 20
        exit 1
    }
}

# 显示应用状态
Write-Host "`n📊 应用状态:" -ForegroundColor Cyan
pm2 status

